

import HeadPrefab from "../../../component/HeadPrefab";
import ComponentBase from "../../../engine/ComponentBase";
import DateUtil from "../../../engine/DateUtil";
import { Logger } from "../../../engine/Logger";
import { HttpHelper } from "../../../net/HttpHelper";
import { ClubConfig } from "../../config/ClubConfig";
import { UserModel } from "../../model/UserModel";
import { UIHelper } from "../../util/UIHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewClubInOutRecord extends ComponentBase {

    private dataList: Array<object> = []
    private defaultItemNode: cc.Node;
    private txtPage: cc.Label
    private page: number = 1
    private totalPage: number = 1
    private pageSize: number = 3

    onEnable(): void {
        super.onEnable();
        this.txtPage = this.getChild(this.node, "txtPage", cc.Label)
        let btnPre: cc.Node = this.findChild(this.node, "btnPre")
        UIHelper.onAddEvent(btnPre, () => {
            if (this.page > 1) {
                this.page--
                this.refresh()
            }
        })
        let btnNext: cc.Node = this.findChild(this.node, "btnNext")
        UIHelper.onAddEvent(btnNext, () => {
            if (this.page < this.totalPage) {
                this.page++
                this.refresh()
            }
        })
        this.defaultItemNode = this.findChild(this.node, "scrollView/view/content/itemNode")
        HttpHelper.get_club_in_out_history(UserModel.instance.nowClub.clubid, (data) => {
            this.dataList = data;
            this.refresh()
        })
        let txtInputUserid: cc.EditBox = this.getChild(this.node, "txtInputUserid", cc.EditBox)
        let btnSearch: cc.Node = this.findChild(this.node, "btnSearch")
        UIHelper.onAddEvent(btnSearch, () => {
            this.onClickSearch(parseInt(txtInputUserid.string))

        })
        this.refresh()
    }
    private onClickSearch(userid: number) {
        let showData: Array<object> = null
        for (let i = 0; i < this.dataList.length; i++) {
            let data: object = this.dataList[i]
            if (data["userid"] == userid) {
                if (!showData) {
                    showData = []
                }
                showData.push(data)
            }
        }
        Logger.log("onClickSearch==", userid, this.dataList, showData)
        this.refresh(showData)
    }


    private refresh(showData: Array<object> = null) {
        this.unscheduleAllCallbacks()
        this.hideAllChildren(this.defaultItemNode.parent)
        let realShowData: Array<object> = []
        if (showData) {
            realShowData = showData
        } else {
            realShowData = this.dataList
        }
        this.totalPage = realShowData.length % this.pageSize == 0 ? realShowData.length / this.pageSize : Math.floor(realShowData.length / this.pageSize) + 1;
        if (this.totalPage <= 0) {
            this.totalPage = 1;
        }
        this.txtPage.string = this.page+"/"+this.totalPage
        // for (let i = 0; i < realShowData.length; i++) {
        for (let i = 0; i < this.pageSize; i++) {
            // let dataObj: object = realShowData[i];
            let realIndex: number = (this.page - 1) * this.pageSize + i
            let dataObj: object = realShowData[realIndex];
            if (dataObj) {
                let item: cc.Node = this.defaultItemNode.parent.children[i]
                if (!item) {
                    this.scheduleOnce(() => {
                        this.initOne(dataObj, i)
                    }, 0.02 * i)
                } else {
                    item.active = true;
                    this.initOne(dataObj, i)
                }
            }
        }
    }

    private initOne(dataObj: object, i: number) {
        let itemNode: cc.Node = this.defaultItemNode.parent.children[i]
        if (!itemNode) {
            itemNode = cc.instantiate(this.defaultItemNode)
            itemNode.parent = this.defaultItemNode.parent;
        }
        itemNode.active = true;
        this.initOneItem(dataObj, itemNode)
    }

    private initOneItem(obj: object, itemNode: cc.Node) {
        itemNode.active = true;
        let head: HeadPrefab = this.getChild(itemNode, "head", HeadPrefab)
        head.setHead(obj["headimg"], obj["lv"])
        let txtName: cc.Label = this.getChild(itemNode, "txtName", cc.Label);
        txtName.string = obj["nick_name"]
        let txtId: cc.Label = this.getChild(itemNode, "txtId", cc.Label)
        txtId.string = obj["userid"] + ""
        let txtVip: cc.Label = this.getChild(itemNode, "txtVip", cc.Label)
        if (obj["invite"]) {
            txtVip.string = "" + obj["invite"]
        } else {
            txtVip.string = ""
        }
        let txtTime: cc.Label = this.getChild(itemNode, "txtTime", cc.Label)
        let time: Date = new Date(obj["time"])
        txtTime.string = DateUtil.formatDateStr(time.getTime())
        let txtInfo: cc.Label = this.getChild(itemNode, "txtInfo", cc.Label);
        let in_out_type: number = obj["in_out_type"]
        let handle_userid: number = obj["handle_userid"]
        if (in_out_type == ClubConfig.in_out_type.be_in) {
            txtInfo.string = handle_userid + "允许进入"
        }
        else if (in_out_type == ClubConfig.in_out_type.be_out) {
            txtInfo.string = handle_userid + "踢走"
        }
        else if (in_out_type == ClubConfig.in_out_type.my_out) {
            txtInfo.string = "主动离开"
        }
    }

}
